package icasue.net.protocol.http.demo;


import icasue.net.protocol.http.HttpOption;
import icasue.net.protocol.http.HttpProtocol;
import icasue.net.protocol.http.HttpResponse;
import icasue.net.protocol.http.utils.SocketUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/3/11 下午8:33
 * @UpdateDate:
 * @Description:
 */
public class Demo {

    /**
     * 文件上传
     * HTTP, POST请求
     * Content-Type: multipart/form-data; boundary=****
     */
    private static void uploadFile(){
        StringBuilder protocol = HttpProtocol.builder(HttpOption.V1_1.POST, "/upload")
                .upFile(new File("/user/shopping.txt"), "shoppingFile")
                .bytes("goodExcel", new byte[12], "good_20200012.xls")
                .build("file.server.com");
        try {
            HttpResponse<Map> send = SocketUtil.send(
                    "file.server.com",
                    80,
                    false,
                    protocol,
                    StandardCharsets.UTF_8,
                    Map.class,
                    3000
            );
            System.out.println("-");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * HTTPS, POST请求
     * Content-Type: application/x-www-form-urlencoded
     */
    private static void getToken(){
        StringBuilder protocol = HttpProtocol.builder(HttpOption.V1_1.POST, "/oauth2/v2/token")
                .wwwFormUrlEncoded("grant_type", "authorization_code")
                .wwwFormUrlEncoded("code", "DwEEAH6SDQptA2o0TJdXSGKmr0WebpuxWm91MxlQluPtZ2c2/33J38YK/DHffviRtRmnEe9Ch51gIA8EgyQQmvn0E/wP5YMTU/HxJd8R416M3F3ToYbwkVBEJpyfdSmDzCr+mLW8W6w6h0jaQpKwzCqkRhPKqCcycMDMtOGlnKBuy0M3YzPpKbN34D6FeRMsojzAJ0mIjo6boFEK8dDl3y1u1ohJtzUjwnon2c40YbxAR7Qw4pC4Zj9NSE+sj5Tx9Gw34O3GndHB80seZ3H+L9Jl")
                .wwwFormUrlEncoded("client_id", "106558733")
                .wwwFormUrlEncoded("client_secret", "f8f28482ad6bc5df752604ba1705336f698fb1d696b71bd9e1c8876a8af6e4ce")
                .wwwFormUrlEncoded("redirect_uri", "http://172.23.61.242:8081/api/hw/cost/auth_callback")
                .build("login.cloud.huawei.com");

        try {
            HttpResponse<String> send = SocketUtil.send(
                    "login.cloud.huawei.com",
                    443,
                    true,
                    protocol,
                    StandardCharsets.UTF_8,
                    String.class,
                    3000
            );
            System.out.println("-");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  HTTPS, POST请求
     *  Content-Type: application/json
     */
    private static void hwShowAdReport(){
        StringBuilder protocol = HttpProtocol.builder(HttpOption.V1_1.POST, "/api/marketing-api/v1/report/ad")
                .header("client_id", "914987258073861120")
                .header("Authorization", "Bearer eyJraWQiOiI2VnFua3NIemg2WnNVT0NMbDVNMjdPeUxCNG9xOUMxbCIsInR5cCI6IkpXVCIsImFsZyI6IkhTMjU2In0.eyJzdWIiOiI5MTQ5ODcyNTgwNzM4NjExMjAiLCJkbiI6MSwiY2xpZW50X3R5cGUiOjEsImV4cCI6MTY1OTYwNzM5NywiaWF0IjoxNjU5NDM0NTk3fQ.q1lH5lRDoClEu-fxhC2ndpRdGi0nzyLTl0uirmqOt1Y")
                .body("{\"startDate\":\"2022-07-05\",\"endDate\":\"2022-08-02\"}")
                .build("connect-api.cloud.huawei.com");
        try {
            HttpResponse<String> send = SocketUtil.send(
                    "connect-api.cloud.huawei.com",
                    443,
                    true,
                    protocol,
                    StandardCharsets.UTF_8,
                    String.class,
                    3000
            );
            System.out.println("-");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * HTTP, GET请求
     * Content-Type: multipart/form-data; boundary=****
     */
    private static void getXxxFormUrlEncoded(){
        StringBuilder protocol = HttpProtocol.builder(HttpOption.V1_1.GET, "/api/hw/cost/auth_callback")
                .form("authorization_code", "00012")
                .form("state", null)
                .build("localhost");
        try {
            HttpResponse<String> send = SocketUtil.send(
                    "localhost",
                    8081,
                    false,
                    protocol,
                    StandardCharsets.UTF_8,
                    String.class,
                    10000
            );
            System.out.println("-");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * HTTP, GET请求
     * URLDecoder.decode(?name=icasue&age=10)
     */
    private static void getQueries(){
        StringBuilder protocol = HttpProtocol.builder(HttpOption.V1_1.GET, "/findUser")
                .query("name", "icasue")
                .query("age", 10)
                .build("name.server.com");
        try {
            HttpResponse<String> send = SocketUtil.send(
                    "name.server.com",
                    8081,
                    false,
                    protocol,
                    StandardCharsets.UTF_8,
                    String.class,
                    10000
            );
            System.out.println("-");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        uploadFile();
        getQueries();
        getXxxFormUrlEncoded();
        getToken();
        hwShowAdReport();
    }
}
